package cst.a110lyj.fragmenttest110lyj.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import cst.a110lyj.fragmenttest110lyj.R;
import cst.a110lyj.fragmenttest110lyj.bean.UserBean;
import cst.a110lyj.fragmenttest110lyj.iface.AmicoolListener;
import cst.a110lyj.fragmenttest110lyj.model.AmicoolModel;

/**
 * Created by admin on 2017/5/26/026.
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener,AmicoolListener<UserBean>{
    private EditText  usename,passwd;
    private Button   button1,button2;
    private CheckBox remCheck;
    private SharedPreferences sp;
    private  CheckBox  checkBox1;
    private void  init(){
       usename = (EditText)findViewById(R.id.et_username);
      passwd = (EditText)findViewById(R.id.et_password);
        button1 =(Button )findViewById(R.id.btn_login);
        button2 =(Button )findViewById(R.id.btn_register);
        sp = getSharedPreferences("login",MODE_PRIVATE);
        checkBox1 = (CheckBox)findViewById(R.id.checkbox_remember);
        if (sp.getBoolean("checkboxBoolean", false))
        {
            usename.setText(sp.getString("username", null));
            passwd.setText(sp.getString("pass", null));
            checkBox1.setChecked(true);

        }

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
      switch (v.getId()){
          case R.id.btn_login:
                  String tmpuser = usename.getText().toString();
                 String tmpppass = passwd.getText().toString();
             AmicoolModel loginModel = new AmicoolModel("gson");
              loginModel.login(tmpuser,tmpppass,this);
                /* if (tmpuser.equals("test")&&tmpppass.equals("test")){
                     Intent intent1 = new Intent(LoginActivity.this,MainActivity.class);
                     startActivity(intent1);
                     finish();
                 }*/
              break;
          case R.id.btn_register:
              Intent intent2 = new Intent(this,RegisterActivity.class);
              startActivity(intent2);
              finish();
              break;
      }
    }


    @Override
    public void onResponse(UserBean userBean) {
        if (userBean.getSessionid()!=null){
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("sessionid",userBean.getSessionid());
            editor.commit();
            String   strname = usename.getText().toString();
            String   strpass =  passwd.getText().toString();
            boolean  Checkboxlogin = checkBox1.isChecked();
            if (Checkboxlogin)
            {
                editor.putString("username", strname);
                editor.putString("pass", strpass);
                editor.putBoolean("checkboxBoolean", true);
                editor.commit();
            }
            else {
                editor.putString("username", null);
                editor.putString("pass", null);
                editor.putBoolean("checkboxBoolean", false);
                editor.commit();

            }
            Intent intent1 = new Intent(this,MainActivity.class);
            startActivity(intent1);
            finish();
        }
        else
            Toast.makeText(this, "cuole ", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(LoginActivity.this,msg, Toast.LENGTH_SHORT).show();
    }
}
